package com.hy.study.practice.linkedlist;

/**
 * 双向链表的练习
 */
public class DoubleLinkedListDemo1 {
    public static void main(String[] args) {
        System.out.println("----------添加数据-------------");
        OrgLinkedList orgLinkedList = new OrgLinkedList();
        Org jt = new Org(1, "集团");
        Org zb = new Org(2, "总部");
        Org zb4 = new Org(2, "总部");
        Org xian = new Org(3, "西安分公司");
        Org chengdu = new Org(4, "成都分公司");
        orgLinkedList.add(jt);
        orgLinkedList.add(zb);
        orgLinkedList.add(zb4);
        orgLinkedList.add(xian);
        orgLinkedList.add(chengdu);
        orgLinkedList.list();

        Org chengdu2 = new Org(4, "成都分公司2");
        System.out.println("---------------update后的----------------");
        orgLinkedList.update(chengdu2);
        orgLinkedList.list();

        System.out.println("--------------delete 后的----------------");
        orgLinkedList.delete(2);
        orgLinkedList.list();
        System.out.println("-------------链表中总个数" + orgLinkedList.size());


        System.out.println("-----------排序添加---------------");


        OrgLinkedList orgLinkedListOrderby = new OrgLinkedList();
        Org jt2 = new Org(1, "集团");

        Org xian2 = new Org(3, "西安分公司");
        Org chengdu3 = new Org(4, "成都分公司");
        Org zb2 = new Org(2, "总部");
        Org zb3 = new Org(2, "总部");
        orgLinkedListOrderby.addOrderBy(jt2);
        orgLinkedListOrderby.addOrderBy(zb2);
        orgLinkedListOrderby.addOrderBy(zb3);
        orgLinkedListOrderby.addOrderBy(xian2);
        orgLinkedListOrderby.addOrderBy(chengdu3);
        orgLinkedListOrderby.list();
    }
}

class OrgLinkedList {
    private Org head = new Org();

    public Org getHead() {
        return head;
    }

    /**
     * 向队列中添加一个链表
     *
     * @param org
     */
    public void add(Org org) {
        Org temp = head;
        while (temp.getNextOrg() != null) {
            if (temp.getNextOrg().getOrgCode() == org.getOrgCode()) {
                System.out.println("已存在相同的元素无法添加");
                return;
            }
            temp = temp.getNextOrg();
        }
        temp.setNextOrg(org);
        org.setPreOrg(temp);
    }

    /**
     * 根据组织结构Code 排序进行操作
     *
     * @param org
     */
    public void addOrderBy(Org org) {
        Org temp = head;
        boolean flag = false;
        while (true) {
            if (temp.getNextOrg() == null) {
                flag = true;
                break;
            }
            if (temp.getNextOrg().getOrgCode() > org.getOrgCode()) {
                flag = true;
                break;
            } else if (temp.getNextOrg().getOrgCode() == org.getOrgCode()) {
                System.out.println("已经存在相同的  无法添加");
                break;
            }
            temp = temp.getNextOrg();
        }
        if (flag) {
            org.setNextOrg(temp.getNextOrg());
            org.setPreOrg(temp);
            temp.setNextOrg(org);
            if (temp.getNextOrg() != null) {//双向链表这里会出现空指针异常问题
                temp.getNextOrg().setPreOrg(org);
            }
        }
    }

    /**
     * 删除要给元素
     *
     * @param orgCode
     */
    public void delete(int orgCode) {
        if (orgCode < 0 || orgCode > size()) {
            return;
        }
        if (head.getNextOrg() == null) {
            return;
        }
        Org temp = head.getNextOrg();
        boolean flag = false;
        while (temp != null) {
            if (temp.getOrgCode() == orgCode) {
                flag = true;
                break;
            }
            temp = temp.getNextOrg();
        }
        if (flag) {
            if (temp.getNextOrg() != null) {
                temp.getNextOrg().setPreOrg(temp.getPreOrg());
            }
            temp.getPreOrg().setNextOrg(temp.getNextOrg());
        }
    }

    /**
     * 获取链表的有效数据的大小
     *
     * @return
     */
    public int size() {
        Org temp = head;
        int count = 0;
        while (temp.getNextOrg() != null) {
            count++;
            temp = temp.getNextOrg();
        }
        return count;
    }

    /**
     * 更新一个元素
     *
     * @param org
     */
    public void update(Org org) {
        if (head.getNextOrg() == null) {
            return;
        }
        Org temp = head.getNextOrg();
        boolean flag = false;
        while (temp != null) {
            if (temp.getOrgCode() == org.getOrgCode()) {
                flag = true;
                break;
            }
            temp = temp.getNextOrg();
        }
        if (flag) {
            temp.setOrgName(org.getOrgName());
        }

    }

    /**
     * 获取一个元素
     *
     * @return
     */
    public Org getOrg(int orgCode) {
        if (head.getNextOrg() == null) {
            return null;
        }
        Org temp = head.getNextOrg();
        boolean flag = false;
        while (temp != null) {
            if (temp.getOrgCode() == orgCode) {
                flag = true;
                break;
            }
            temp = temp.getNextOrg();
        }
        if (flag) {
            return temp;
        }
        return null;
    }

    /**
     * 展示所有元素
     */
    public void list() {
        if (head.getNextOrg() == null) {
            System.out.println("链表为空");
            return;
        }
        Org temp = head.getNextOrg();
        while (temp != null) {
            System.out.println(temp);
            temp = temp.getNextOrg();
        }
    }
}

class Org {
    private int orgCode;
    private String orgName;
    private Org nextOrg;
    private Org preOrg;

    public Org() {
    }

    public Org(int orgCode, String orgName) {
        this.orgCode = orgCode;
        this.orgName = orgName;
    }

    public int getOrgCode() {
        return orgCode;
    }

    public void setOrgCode(int orgCode) {
        this.orgCode = orgCode;
    }

    public String getOrgName() {
        return orgName;
    }

    public void setOrgName(String orgName) {
        this.orgName = orgName;
    }

    public Org getNextOrg() {
        return nextOrg;
    }

    public void setNextOrg(Org nextOrg) {
        this.nextOrg = nextOrg;
    }

    public Org getPreOrg() {
        return preOrg;
    }

    public void setPreOrg(Org preOrg) {
        this.preOrg = preOrg;
    }

    @Override
    public String toString() {
        return "Org{" +
                "orgCode=" + orgCode +
                ", orgName='" + orgName + '\'' +
                '}';
    }
}
